<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--组织数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
              v-model="deptName"
              placeholder="请输入组织名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
              :data="deptOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              node-key="id"
              highlight-current
              @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="工号" prop="GH">
            <el-input
                v-model="queryParams.GH"
                placeholder="请输入工号"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="姓名" prop="XM">
            <el-input
                v-model="queryParams.XM"
                placeholder="请输入姓名"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
<!--          <el-form-item label="当前状态" prop="DQZTM">-->
<!--            <el-input-->
<!--                v-model="queryParams.DQZTM"-->
<!--                placeholder="请输入当前状态"-->
<!--                clearable-->
<!--                @keyup.enter.native="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
          <el-form-item label="职务名称" prop="FILED9">
            <el-input
                v-model="queryParams.FILED9"
                placeholder="请输入职务名称"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
<!--          <el-form-item label="单位号" prop="DWH">-->
<!--            <el-input-->
<!--                v-model="queryParams.DWH"-->
<!--                placeholder="请输入单位号"-->
<!--                clearable-->
<!--                @keyup.enter.native="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="院系代码" prop="ZWMC">-->
<!--            <el-input-->
<!--                v-model="queryParams.ZWMC"-->
<!--                placeholder="请输入院系代码"-->
<!--                clearable-->
<!--                @keyup.enter.native="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="专业名称" prop="RN">-->
<!--            <el-input-->
<!--                v-model="queryParams.RN"-->
<!--                placeholder="请输入专业名称"-->
<!--                clearable-->
<!--                @keyup.enter.native="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
          <!--          <el-form-item label="${comment}" prop="BJMC">-->
          <!--            <el-input-->
          <!--              v-model="queryParams.BJMC"-->
          <!--              placeholder="请输入${comment}"-->
          <!--              clearable-->
          <!--              @keyup.enter.native="handleQuery"-->
          <!--            />-->
          <!--          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['edu:teacher:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="teacherList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="工号" align="center" prop="gh" />
          <el-table-column label="姓名" align="center" prop="xm" />
          <el-table-column label="当前状态" align="center" prop="dqztm" >
            <template #default="scope">
              <span>{{ scope.row.dqztm == '11' ? '在职' : '' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="职务名称" align="center" prop="zwmc" />
          <el-table-column label="单位号" align="center" prop="dwh" />
          <el-table-column label="身份证号码" align="center" prop="sfzjh" />
<!--          <el-table-column label="院系代码" align="center" prop="dwh" />-->
<!--          <el-table-column label="专业名称" align="center" prop="rn" />-->
          <!--          <el-table-column label="${comment}" align="center" prop="remark" />-->
          <!--          <el-table-column label="${comment}" align="center" prop="bjmc" />-->
<!--          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--            <template slot-scope="scope">-->
<!--              <el-button-->
<!--                  size="mini"-->
<!--                  type="text"-->
<!--                  icon="el-icon-edit"-->
<!--                  @click="handleUpdate(scope.row)"-->
<!--                  v-hasPermi="['edu:teacher:edit']"-->
<!--              >修改</el-button>-->
<!--              <el-button-->
<!--                  size="mini"-->
<!--                  type="text"-->
<!--                  icon="el-icon-delete"-->
<!--                  @click="handleDelete(scope.row)"-->
<!--                  v-hasPermi="['edu:teacher:remove']"-->
<!--              >删除</el-button>-->
<!--            </template>-->
<!--          </el-table-column>-->
        </el-table>
        <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改老师教职工对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="id">
          <el-input v-model="form.id" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="工号" prop="GH">
          <el-input v-model="form.GH" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="姓名" prop="XM">
          <el-input v-model="form.XM" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="当前状态" prop="DQZTM">
          <el-input v-model="form.DQZTM" placeholder="请输入当前状态" />
        </el-form-item>
        <el-form-item label="职务名称" prop="FILED9">
          <el-input v-model="form.FILED9" placeholder="请输入职务名称" />
        </el-form-item>
        <el-form-item label="单位号" prop="DWH">
          <el-input v-model="form.DWH" placeholder="请输入单位号" />
        </el-form-item>
        <el-form-item label="院系代码" prop="ZWMC">
          <el-input v-model="form.ZWMC" placeholder="请输入院系代码" />
        </el-form-item>
        <el-form-item label="专业名称" prop="RN">
          <el-input v-model="form.RN" placeholder="请输入专业名称" />
        </el-form-item>
        <el-form-item label="${comment}" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="BJMC">
          <el-input v-model="form.BJMC" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>


<script setup name="User">
import { getToken } from "@/utils/auth";
import { listTeacher, getTeacher, delTeacher, addTeacher, updateTeacher } from "@/api/edu/teacher";
import { getTreeData } from "@/api/edu/org";
const router = useRouter();
const { proxy } = getCurrentInstance();
const { sys_normal_disable, sys_user_sex } = proxy.useDict("sys_normal_disable", "sys_user_sex");

const teacherList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const deptName = ref("");
const deptOptions = ref(undefined);
const initPassword = ref(undefined);
const postOptions = ref([]);
const roleOptions = ref([]);
/*** 用户导入参数 */
const upload = reactive({
  // 是否显示弹出层（用户导入）
  open: false,
  // 弹出层标题（用户导入）
  title: "",
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/system/user/importData"
});
// 列显隐信息
const columns = ref([

]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: undefined,
    phonenumber: undefined,
    status: undefined,
    DWH: undefined
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 通过条件过滤节点  */
const filterNode = (value, data) => {
  if (!value) return true;
  return data.label.indexOf(value) !== -1;
};
/** 根据名称筛选组织树 */
watch(deptName, val => {
  proxy.$refs["deptTreeRef"].filter(val);
});
/** 查询组织下拉树结构 */
function getDeptTree() {
  getTreeData().then(response => {
    deptOptions.value = response;
  });
};
/** 查询用户列表 */
function getList() {
  loading.value = true;
  listTeacher(proxy.addDateRange(queryParams.value, dateRange.value)).then(res => {
    loading.value = false;
    teacherList.value = res.rows;
    total.value = res.total;
  });
};
/** 节点单击事件 */
function handleNodeClick(data) {
  queryParams.value.DWH = data.id;
  handleQuery();
};
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.DWH = undefined;
  proxy.$refs.deptTreeRef.setCurrentKey(null);
  handleQuery();
};
/** 删除按钮操作 */
function handleDelete(row) {
  const userIds = row.userId || ids.value;
  proxy.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function () {
    return delUser(userIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
};
/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/user/export", {
    ...queryParams.value,
  },`user_${new Date().getTime()}.xlsx`);
};
/** 跳转角色分配 */
function handleAuthRole(row) {
  const userId = row.userId;
  router.push("/system/user-auth/role/" + userId);
};
/** 重置密码按钮操作 */
function handleResetPwd(row) {
  proxy.$prompt('请输入"' + row.userName + '"的新密码', "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    closeOnClickModal: false,
    inputPattern: /^.{5,20}$/,
    inputErrorMessage: "用户密码长度必须介于 5 和 20 之间",
  }).then(({ value }) => {
    resetUserPwd(row.userId, value).then(response => {
      proxy.$modal.msgSuccess("修改成功，新密码是：" + value);
    });
  }).catch(() => {});
};
/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};
/** 重置操作表单 */
function reset() {
  form.value = {
    userId: undefined,
    DWH: undefined,
    userName: undefined,
    nickName: undefined,
    password: undefined,
    phonenumber: undefined,
    email: undefined,
    sex: undefined,
    status: "0",
    remark: undefined,
    postIds: [],
    roleIds: []
  };
  proxy.resetForm("userRef");
};
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
};

getDeptTree();
getList();
</script>
